import request from "../../utils/request";

Page({
  data: {
    bannerList: [],   // 轮播图数据
    recommendList: [],   // 推荐数据
    topList: [],   // 排行榜数据
  },
  onLoad: function() {
    // 获取轮播图数据
    this.getBannerData();

    // 获取推荐数据
    this.getRecommendData();

    // 获取排行榜数据
    this.getTopListData();
  },

  // 跳转到每日推荐页面
  goRecommendSong(){
    wx.navigateTo({
      url: '/packageB/pages/recommendSong/recommendSong'
    })
  },

  // 跳转到other页面
  goOther(){
    wx.navigateTo({
      url: '/packageA/pages/other/other'
    })
  },

  // 获取排行榜数据
  async getTopListData(){
    let index = 0;
    let newList = [];
    while(index<5){
      let res = await request('/top/list',{idx: index++});
      if (res.code === 200) {
        newList.push({
          name: res.playlist.name,
          tracks: res.playlist.tracks.slice(0,3),
          id: res.playlist.id
        });
        this.setData({
          topList: newList,
        })
      }
    }
  },

  // 获取轮播图数据
  async getBannerData(){
    let res = await request('/banner');
    if (res.code === 200){
      this.setData({
        bannerList: res.banners
      })
    }
  },

  // 获取推荐数据
  async getRecommendData(){
    let res = await request('/personalized', {limit: 10});
    if (res.code === 200){
      this.setData({
        recommendList: res.result
      })
    }
  }

})
